package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.CarDAO;
import com.wanmait.hotelManage.vo.Car;
import com.wanmait.hotelManage.vo.Pager;

/**
 * Servlet implementation class CarServlet
 */
@WebServlet("/manage/CarServlet")
public class CarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("update".equals(action)) {
			this.update(request, response);
		}else if("update1".equals(action)) {
			this.update1(request, response);
		}else if("inittwo".equals(action)) {
			this.inittwo(request, response);
		}else if("del".equals(action)) {
			this.del(request, response);
		}else if("insert".equals(action)){
			this.insert(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("searchTwo".equals(action)) {
			this.searchTwo(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 搜索分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Integer pageNo=Integer.parseInt(request.getParameter("pageNo"));
		Boolean flag=true;
		if("1".equals(text)) {
			flag=false;
		}
		Pager pager=new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(10);
		int rows=CarDAO.getCarDAO().getrowss(flag);
		pager.setPageCount(rows);
		List<Car>cars=CarDAO.getCarDAO().findcheck(flag,pager);
		request.setAttribute("cars", cars);
		request.setAttribute("pager", pager);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/car/searchTwo.jsp").forward(request, response);
	}
	/**
	 * 搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Boolean flag=true;
		if("1".equals(text)) {
			flag=false;
		}
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(10);
		int rows=CarDAO.getCarDAO().getrowss(flag);
		pager.setPageCount(rows);
		List<Car>cars=CarDAO.getCarDAO().findcheck(flag,pager);
		request.setAttribute("cars", cars);
		request.setAttribute("pager", pager);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/car/search.jsp").forward(request, response);
	}
	/**
	 * 添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String number=request.getParameter("title");
//		String check=request.getParameter("check");
//		Double price=Double.parseDouble(request.getParameter("price"));
		Double price=100.0;
//		Boolean flag=false;
//		if("正在使用".equals(check)) {
//			flag=true;
//		}
		CarDAO.getCarDAO().insert(number,price);
		response.sendRedirect(request.getContextPath()+"/manage/CarServlet?action=init");
	}
	/**
	 * 分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void inittwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("pageNo"));
		Pager pager=new Pager();
		pager.setPageNo(id);
		pager.setPageSize(10);
		int rows=CarDAO.getCarDAO().getrows();
		pager.setPageCount(rows);
		List<Car>cars=CarDAO.getCarDAO().findAll(pager);
		request.setAttribute("cars", cars);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/car/initTwo.jsp").forward(request, response);
	}
	/**
	 * 进行更新,不确定要使用
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
//	protected void update1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Integer id=Integer.parseInt(request.getParameter("id"));
//		String check=request.getParameter("check");
////		Double price=Double.parseDouble(request.getParameter("price"));
//		Boolean flag1=true; 
//		if("空闲".equals(check)) {
//			flag1=false;
//		}
//		CarDAO.getCarDAO().update(flag1,id);
//		response.sendRedirect(request.getContextPath()+"/manage/CarServlet?action=init");
//	}
	/**
	 * 找到关于这个id的所有内容给update,不确定要使用
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
//	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		Integer id=Integer.parseInt(request.getParameter("id"));
//		Car car=CarDAO.getCarDAO().findthis(id);
//		request.setAttribute("car", car);
//		request.getRequestDispatcher("/manage/car/update.jsp").forward(request, response);
//	}
	/**
	 * 修改价钱
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Double price=Double.parseDouble(request.getParameter("price"));
	CarDAO.getCarDAO().updatePrice(price);
	response.sendRedirect(request.getContextPath()+"/manage/CarServlet?action=init");
	}
	/**
	 * 找到价钱
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Double price=CarDAO.getCarDAO().findPrice();
	request.setAttribute("price", price);
	request.getRequestDispatcher("/manage/car/updatePrice.jsp").forward(request, response);
	}
	
	/**
	 * ajax的删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		CarDAO.getCarDAO().del(id);
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(10);
		int rows=CarDAO.getCarDAO().getrows();
		pager.setPageCount(rows);
		List<Car>cars=CarDAO.getCarDAO().findAll(pager);
		request.setAttribute("cars", cars);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/car/list.jsp").forward(request, response);
	}
}
